/*
//  RCChoiceFileHandler.m
//  Choice
//
//  Created by Rhys Cox on 31/08/2007.
//  Copyright 2007 Rhys Cox. All rights reserved.
*/

#import "RCChoiceFileHandler.h"


@implementation RCChoiceFileHandler
- (int)getTotalRightAnswers:(NSString*)path
{
	NSDictionary *tempPlistDict = [NSDictionary dictionaryWithContentsOfFile:path];
	NSArray* rArray = [[NSArray alloc] initWithArray:[tempPlistDict objectForKey:@"Right"]];
	//Grabs 'Right' array from plist 'path'
	return [rArray count];
}

- (int)getTotalWrongAnswers:(NSString*)path
{
	NSDictionary *tempPlistDict = [NSDictionary dictionaryWithContentsOfFile:path];
	NSArray* wArray = [[NSArray alloc] initWithArray:[tempPlistDict objectForKey:@"Wrong"]];
	//Grabs 'Wrong' array from plist 'path'
	return [wArray count];
}

- (NSMutableArray*)getAnswers:(NSString*)path
{
	//Load plist into tempPlistDict
	NSDictionary *tempPlistDict = [NSDictionary dictionaryWithContentsOfFile:path];
	//Create arrays containing 'Right' and 'Wrong' arrays from plist
	NSArray* rArray = [[NSArray alloc] initWithArray:[tempPlistDict objectForKey:@"Right"]];
	NSArray* wArray = [[NSArray alloc] initWithArray:[tempPlistDict objectForKey:@"Wrong"]];
	//Create dictionary to hold answers array, and temporary answers array used in loop
	NSMutableArray* Answers = [[NSMutableArray alloc] init];
	NSMutableDictionary *ans = [[NSMutableDictionary alloc] init];
	//Create two variables to power the loop
	int count;
	int condition;
	/*Compare total right and wrong answers to determine when the loop should end
	  This enables there to be more right than wrong answers, and more wrong than right*/
	if([rArray count] >= [wArray count]){
		condition = [rArray count];
	}
	else{
		condition = [wArray count];
	}
	for(count = 0;count < condition;count++){
		ans = [[NSMutableDictionary alloc] init];
		//Re-init temporary dictionary to hold new answer pair (MUST BE PAIRED FOR LISTBOX TO OPERATE CORRECTLY)
		if(count < [rArray count]){
			[ans setObject:[rArray objectAtIndex:count] forKey:@"Correct"];
		}
		if(count < [wArray count]){
			[ans setObject:[wArray objectAtIndex:count] forKey:@"Incorrect"];
		}
		//if there are items in either array left to be added to the 'ans' array - add them, otherwise move along
		[Answers addObject:ans];
		//add answer pair in temporary dictionary to 'Answers' dictionary
	}
	return Answers;
}

- (NSString*)getResultPath
{
	return ResultsPath;
}

- (BOOL)saveResults:(NSArray*)rightAnswers wrongAnswers:(NSArray*)wrongAnswers testInfo:(NSArray*)testInfo
{
	NSMutableDictionary *dictMain = [[NSMutableDictionary alloc] init];
	//Create temporary dictionary to hold values
	[dictMain setObject:rightAnswers forKey:@"Right"];
	[dictMain setObject:wrongAnswers forKey:@"Wrong"];
	[dictMain setObject:[testInfo objectAtIndex:1] forKey:@"Subject"];
	[dictMain setObject:[testInfo objectAtIndex:2] forKey:@"Level"];
	//Fill dictionary with data
	NSString *path;
	if([[NSUserDefaults standardUserDefaults] objectForKey:@"saveResultsHome"] == @"1"){
		//Auto generate a path in the user's home directory
		path = [[[[[NSHomeDirectory() stringByAppendingString:@"/"] stringByAppendingString:[testInfo objectAtIndex:1]] stringByAppendingString:@" - "] stringByAppendingString:[testInfo objectAtIndex:2]] stringByAppendingString:@".cresults"];
	}
	else{
		//Show a save panel and set the path to the result
		NSSavePanel *sPanel = [NSSavePanel savePanel];
		NSArray *fileTypes = [NSArray arrayWithObjects:@"cresults",nil];
		[sPanel setCanCreateDirectories:YES];
		[sPanel setAllowedFileTypes:fileTypes];
		[sPanel setAllowsOtherFileTypes:NO];
		[sPanel setTitle:@"Save your test results"];
		if([sPanel runModalForDirectory:NSHomeDirectory() file:nil] == NSOKButton){
			path = [sPanel filename];
			ResultsPath = path;
		}
		else{
			/*If cancel or failure abort the whole function - no workaround found without causing spaghetti code
			  may need to rewrite this function at a later date*/
			return NO;
		}
	}
	NSDictionary *dictSave = [[NSDictionary alloc] initWithDictionary:dictMain];
	//Create a static dictionary to be used for saving
	return [dictSave writeToFile:path atomically:YES];
	//Return result of saving to plist file structure
}

- (int)loadTest
{
	//Create array of acceptible files
	NSArray *fileTypes = [NSArray arrayWithObjects:@"ctest",nil];
	oPanel = [NSOpenPanel openPanel];
	[oPanel setCanChooseDirectories:NO];
	[oPanel setCanChooseFiles:YES];
	[oPanel setCanCreateDirectories:YES];
	[oPanel setAllowsMultipleSelection:NO];
	[oPanel setTitle:@"Select a test file to open"];
	//Show open panel
	if([oPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes] == NSOKButton){
		return YES;
	}
	else{
		//Abort function if cancel or failure
		return NO;
	}
}

- (NSArray*)getTestQA;
{
	//Create temporary array to hold questions and correct answers
	NSMutableArray* tempQAArray = [[NSMutableArray alloc] init];
	//Create array to hold selected files' names
	NSArray *fileDir = [oPanel filenames];
	NSDictionary *tempPlistDict = [NSDictionary dictionaryWithContentsOfFile:[fileDir objectAtIndex:0]];
	//Create dictionary holding contents of test plist
	NSArray* qArray = [[NSArray alloc] initWithArray:[tempPlistDict objectForKey:@"Questions"]];
	NSArray* aArray = [[NSArray alloc] initWithArray:[tempPlistDict objectForKey:@"Answers"]];
	//Create arrays holding question and answer data from plist
	int count;
	for(count = 0;count < [qArray count];count++){
		[tempQAArray addObject:[qArray objectAtIndex:count]];
		[tempQAArray addObject:[aArray objectAtIndex:count]];
	}
	//Fill temporary array with alternating values from both arrays (WILL NEED CHANGING FOR QA RANDOMISATION)
	NSArray* qaArray = [[NSArray alloc] initWithArray:tempQAArray];
	//Create static array holding temporary array contents and return it
	return qaArray;
}

- (NSArray*)getTestInfo;
{
	NSMutableArray* tempInfoArray = [[NSMutableArray alloc] init];
	//Create temporary array to hold plist contents
	NSArray *fileDir = [oPanel filenames];
	NSDictionary *tempPlistDict = [NSDictionary dictionaryWithContentsOfFile:[fileDir objectAtIndex:0]];
	//Fill temporary dictionary with contents of fileDir's first item
	[tempInfoArray addObject:[tempPlistDict valueForKey:@"Creator"]];
	[tempInfoArray addObject:[tempPlistDict valueForKey:@"Subject"]];
	[tempInfoArray addObject:[tempPlistDict valueForKey:@"Level"]];
	NSArray* infoArray = [[NSArray alloc] initWithArray:tempInfoArray];
	//Fill temporary array with values and migrate to static array
	return infoArray;
	//Return static array
}

@end
